﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Model;
using System.Data;
using System.Data.SqlClient;

namespace Controller
{
    public class JobPositionController : Controller.dbConnection.ConnectionClass
    {
        public string Save(JobPosition JobPositions)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "UP_INSERTUPDATEJOBPOSITION";
            command.Parameters.Add("@ID", SqlDbType.VarChar).Value = JobPositions.ID;
            command.Parameters.Add("@POSITION", SqlDbType.VarChar).Value = JobPositions.Position;
            command.Parameters.Add("@JOBDESCRIPTION", SqlDbType.VarChar).Value = JobPositions.JobDescription;
            command.Parameters.Add("@ISACTIVE", SqlDbType.VarChar).Value = JobPositions.isActive;
            command.Parameters.Add("@LASTMODIFIEDBY", SqlDbType.VarChar).Value = JobPositions.LastModifiedBy;
            command.Parameters.Add("@CREATEBY", SqlDbType.VarChar).Value = JobPositions.CreateBy;
            command.Parameters.Add("@BusinessGroupID", SqlDbType.VarChar).Value = JobPositions.BusinessGroupID;

            return ExecuteNonQuery(command);
        }

        public string Delete(JobPosition JobPositions)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "UP_DELETEJOBPOSITION";
            command.Parameters.Add("@ID", SqlDbType.VarChar).Value = JobPositions.ID;
            command.Parameters.Add("@LASTMODIFIEDBY", SqlDbType.VarChar).Value = JobPositions.LastModifiedBy;

            return ExecuteNonQuery(command);
        }

        public string BeforeDelete(JobPosition JobPositions)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_CheckBeforeDeleteJobPosition";
            command.Parameters.Add("@ID", SqlDbType.Int).Value = JobPositions.ID;
            

            return GetSingleData(command);
        }

        public IList<JobPosition> GetAll(string sSearch, int PageNumber, int Datalength, string SortCol, string SortDirection, out int TotalData, int BusinessGroupID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_GetAllJobPosition";
            command.Parameters.Add("@SEARCH", SqlDbType.VarChar).Value = sSearch;
            command.Parameters.Add("@BusinessGroupID", SqlDbType.VarChar).Value = BusinessGroupID;

            DataSet dx = GetTable(command);

            List<JobPosition> JobPositions = new List<JobPosition>();

            switch (SortDirection)
            {
                case "asc":
                    switch (SortCol)
                    {
                        case "Position":
                            dx.Tables[0].DefaultView.Sort = "Position ASC";
                            break;
                        case "JobDescription":
                            dx.Tables[0].DefaultView.Sort = "JobDescription ASC";
                            break;
                        case "LastModifiedDate":
                            dx.Tables[0].DefaultView.Sort = "LastModifiedDate ASC";
                            break;
                        case "LastModifiedBy":
                            dx.Tables[0].DefaultView.Sort = "LastModifiedBy ASC";
                            break;
                        case "CreateDate":
                            dx.Tables[0].DefaultView.Sort = "CreateDate ASC";
                            break;
                        case "CreateBy":
                            dx.Tables[0].DefaultView.Sort = "CreateBy ASC";
                            break;
                    }
                    break;
                case "desc":
                    switch (SortCol)
                    {
                        case "Position":
                            dx.Tables[0].DefaultView.Sort = "Position DESC";
                            break;
                        case "JobDescription":
                            dx.Tables[0].DefaultView.Sort = "JobDescription DESC";
                            break;
                        case "LastModifiedDate":
                            dx.Tables[0].DefaultView.Sort = "LastModifiedDate DESC";
                            break;
                        case "LastModifiedBy":
                            dx.Tables[0].DefaultView.Sort = "LastModifiedBy DESC";
                            break;
                        case "CreateDate":
                            dx.Tables[0].DefaultView.Sort = "CreateDate DESC";
                            break;
                        case "CreateBy":
                            dx.Tables[0].DefaultView.Sort = "CreateBy DESC";
                            break;
                    }
                    break;
            }

            DataTable dt = dx.Tables[0].DefaultView.ToTable();

            if (dt.Rows.Count > 0)
            {
                for (int i = PageNumber; i < Datalength; i++)
                {
                    if (i < dt.Rows.Count)
                    {
                        JobPosition p = new JobPosition();
                        p.ID = Convert.ToInt16(dt.Rows[i]["ID"].ToString());
                        p.Position = dt.Rows[i]["Position"].ToString();
                        p.JobDescription = dt.Rows[i]["JobDescription"].ToString();
                        p.isActive = Convert.ToBoolean(dt.Rows[i]["isActive"].ToString());
                        p.LastModifiedBy = dt.Rows[i]["LastModifiedBy"].ToString();
                        p.LastModifiedDate = dt.Rows[i]["LastModifiedDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(dt.Rows[i]["LastModifiedDate"].ToString());
                        p.CreateBy = dt.Rows[i]["CreateBy"].ToString();
                        p.CreateDate = dt.Rows[i]["CreateDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(dt.Rows[i]["CreateDate"].ToString());
                        JobPositions.Add(p);
                    }
                    else
                    {
                        break;
                    }

                }
            }

            TotalData = dt.Rows.Count;

            return JobPositions.ToList();
        }

        public JobPosition GetJobPositionByID(int ID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_GETJOBPOSITIONBYID";
            command.Parameters.Add("@ID", SqlDbType.Int).Value = ID;

            IList<string> Data = GetSingleRow(command);
            JobPosition JobPositions = null;

            if (Data != null && Data.Count > 0)
            {
                JobPositions = new JobPosition();
                JobPositions.ID = Convert.ToInt16(Data[0]);
                JobPositions.Position = Data[1].ToString();
                JobPositions.JobDescription = Data[2].ToString();
                JobPositions.isActive = Convert.ToBoolean(Data[3]);
                JobPositions.LastModifiedDate = Data[4] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[4].ToString());
                JobPositions.LastModifiedBy = Data[5].ToString();
                JobPositions.CreateDate = Data[6] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[6].ToString());
                JobPositions.CreateBy = Data[7].ToString();
            }

            return JobPositions;
        }

        public JobPosition GetJobPositionByDescription(string JobPositionDescription)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_GETJOBPOSITIONBYDESCRIPTION";
            command.Parameters.Add("@DESCRIPTION", SqlDbType.VarChar).Value = JobPositionDescription;

            IList<string> Data = GetSingleRow(command);
            JobPosition JobPositions = null;

            if (Data != null && Data.Count > 0)
            {
                JobPositions = new JobPosition();
                JobPositions.ID = Convert.ToInt16(Data[0]);
                JobPositions.Position = Data[1].ToString();
                JobPositions.JobDescription = Data[2].ToString();
                JobPositions.isActive = Convert.ToBoolean(Data[3]);
                JobPositions.LastModifiedDate = Data[4] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[4].ToString());
                JobPositions.LastModifiedBy = Data[5].ToString();
                JobPositions.CreateDate = Data[6] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[6].ToString());
                JobPositions.CreateBy = Data[7].ToString();
            }

            return JobPositions;
        }
    }
}